package cn.cmcc.online.smsapi.sdk.util;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import cn.cmcc.online.smsapi.sdk.Iservice.XyCallBack;
import cn.cmcc.online.smsapi.sdk.constant.Constant;
import cn.cmcc.online.smsapi.sdk.db.entity.SysParamEntityManager;
import cn.cmcc.online.smsapi.sdk.db.entity.menuActionManager;
import cn.cmcc.online.smsapi.sdk.log.LogManager;
import cn.cmcc.online.smsapi.sdk.net.NetUtil;
import cn.cmcc.online.smsapi.sdk.net.NewXyHttpRunnable;

public class MenuActionCountActionUtil {

    public static void postMenuCountAction() {
        boolean ispost = false;
        // 周期
        final String curTime = DateUtils.getCurrentTimeString("yyyyMMdd");// 当前日期
        String lastPostTime = getLastSceneCountActionTime(Constant.getContext());
        if (lastPostTime == null) {// 第一次上报

            ispost = true;
        } else {

            ispost = DateUtils.compareDateString(// 间隔大于一天，把今天之前的上传
                    curTime, DateUtils.addDays(lastPostTime, "yyyyMMdd", 1),// 增加一天
                    "yyyyMMdd");
        }

        NetUtil.requestNewTokenIfNeed(null);
        if (ispost) {
            try {
                final String reportData = menuActionManager.queryDataByDate(curTime).toString();
                if (!StringUtils.isNull(reportData)) {
                    /* SDK-526 huangzhiqiang 20160713 start */
                    final String token = NetUtil.getToken();
                    /* SDK-526 huangzhiqiang 20160713 end */
                    XyCallBack callBack = new XyCallBack() {

                        @Override
                        public void execute(Object... obj) {
                            if (obj != null && obj.length > 0) {
                                String resultCode = obj[0].toString();
                                if (resultCode.equals(NewXyHttpRunnable.ERROR_CODE_TOKEN_FAILED)) {
                                    /* SDK-526 huangzhiqiang 20160713 start */
                                    NetUtil.requestNewTokenAndPostRequestAgain(NetUtil.URL_MENU_CLICKED, reportData,
                                            Constant.FIVE_MINUTES, true, false, true, null, token, this);
                                    return;
                                    /* SDK-526 huangzhiqiang 20160713 end */
                                } 

                                if (resultCode.equals("0") && obj.length == 2) {
                                    String response = obj[1].toString();
                                    // 解析返回的报文
        							try {
        								JSONObject jsonObject = new JSONObject(response);
        								jsonObject.get("code");
        								if (jsonObject.get("code").equals("200")){
        									  // 记录上报的日期
        									setLastSceneCountActionTime(Constant.getContext(), curTime);
                                            // 删除数据
                                            menuActionManager.deleteInfoByDate(curTime);
        								}
        							} catch (JSONException e) {
        								 LogManager.e(Constant.TAG, "MenuActionCountActionUatil: ", e);
        							}
                                }
                            }
                        }
                    };

                    NetUtil.executeNewServiceHttpRequest(NetUtil.URL_MENU_CLICKED, reportData, callBack, true, false, true, null);

                }
            } catch (Throwable e) {
            	 LogManager.e(Constant.TAG, "MenuActionCountActionUatil: ", e);
            }
        }
    }

//    public static String getReportDataString(String updateDate) {
//    	
//    	
//        return menuActionManager.queryDataByDate(updateDate).toString();
//    }

    
    public static String getLastSceneCountActionTime(Context context) {

        return SysParamEntityManager.getStringParam(context,
                "LastMenuActionCountActionUpdate");
    }

    public static void setLastSceneCountActionTime(Context context,
            String curDate) {

        SysParamEntityManager.setParam("LastMenuActionCountActionUpdate", curDate);
    }

}
